{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "# client_id 为官网获取的API key， client_secret 为官网获取的Secret key\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={官网获取的AK}&client_secret={官网获取的SK}'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.人脸检测与属性分析\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3430692674,459091344&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(request_url, data=params, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 7565651012016,\n",
       " 'timestamp': 1603538460,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "    'location': {'left': 186.93,\n",
       "     'top': 99.21,\n",
       "     'width': 121,\n",
       "     'height': 118,\n",
       "     'rotation': 1},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -1.85, 'pitch': 4.67, 'roll': 0.97},\n",
       "    'face_shape': {'type': 'oval', 'probability': 0.66},\n",
       "    'face_type': {'type': 'human', 'probability': 1}}]}}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.人脸对比\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3430692674,459091344&fm=26&gp=0.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603015508446&di=eee4e2c852d804bc3b80a719df3df9ef&imgtype=0&src=http%3A%2F%2Fimg2-cloud.itouchtv.cn%2Ftvtouchtv%2Fimage%2F20170914%2F1505363583630378.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "# face_type 人脸的类型 LIVE；IDCARD；WATERMARK；CERT；INFRARED。\n",
    "\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(request_url, data=params, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 3555942589001,\n",
       " 'timestamp': 1603538608,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 94.37832642,\n",
       "  'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c'},\n",
       "   {'face_token': '93d2501e1cb1685c26107d0b19cc854a'}]}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.创建用户组\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/add\"\n",
    "\n",
    "params = \"{\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(request_url, data=params, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 223101,\n",
       " 'error_msg': 'group already exist',\n",
       " 'log_id': 7994251594250,\n",
       " 'timestamp': 1603538786,\n",
       " 'cached': 0,\n",
       " 'result': None}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.获取用户人脸列表\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist\"\n",
    "\n",
    "params = \"{\\\"user_id\\\":\\\"user1\\\",\\\"group_id\\\":\\\"group2\\\"}\"\n",
    "access_token = '24.6d684a22feb8384590448aa0f4edcb8e.2592000.1605596154.282335-22838595' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(request_url, data=params, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 19900165158,\n",
       " 'timestamp': 1603538912,\n",
       " 'cached': 0,\n",
       " 'result': {'face_list': [{'face_token': '91cf0a5aa45b0371989e56760b30548c',\n",
       "    'ctime': '2020-10-23 17:46:47'}]}}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
